package top.datacluster.basic.algorithm.leetcode.tree;

import top.datacluster.basic.algorithm.leetcode.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class LeetCode1569 {

    public int[] levelOrder(TreeNode root) {
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.addLast(root);
        List<Integer> temp = new ArrayList<>();
        while (queue.size() != 0){
            TreeNode node = queue.removeFirst();
            temp.add(node.val);
            if (node.left != null){
                queue.addLast(node.left);
            }
            if (node.right != null){
                queue.addLast(node.right);
            }
        }
        int[] result = new int[temp.size()];
        for (int i = 0;i < temp.size(); i++){
            result[i] = temp.get(i);
        }

        return result;
    }

    public static void main(String[] args) {
        TreeNode node = new TreeNode(10);
        TreeNode node1 = new TreeNode(12);
        TreeNode node2 = new TreeNode(6);
        TreeNode node3 = new TreeNode(8);
        TreeNode node4 = new TreeNode(3);
        TreeNode node5 = new TreeNode(11);
        node.left = node1;
        node.right = node2;
        node1.left = node3;
        node1.right = node4;
        node2.left = node5;
        new LeetCode1569().levelOrder(node);
    }
}
